﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DxMvcApplication.Areas.Administrator.Models.Dictionary;
using DxMvcApplication.Areas.Administrator.Models.Report;
using DxMvcApplication.Repositories.Implementations.Dictionary;

namespace DxMvcApplication.Areas.Administrator.Controllers.Dictionary
{
    public class ReportListController : Controller
    {
        private readonly ReportListRepository _departmentRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="ReportListController"/> class.
        /// </summary>
        public ReportListController()
        {
            _departmentRepository = new ReportListRepository();
        }

        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View("ReportList");
        }

        public ActionResult GetPaged(int? page)
        {
            var pageIndex = page ?? 1;
            var totalPage = _departmentRepository.GetPageSize();
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = pageIndex;
            var departments = _departmentRepository.GetPaged(ref totalPage, pageIndex);
            return View("ReportListList", departments.ToModels());
        }

        public ActionResult Detail(int? id)
        {
            if (id != null)
            {
                var department = _departmentRepository.Get((int)id);
                //    return View("ReportListDetail", department.ToModel());
            }
            return View("ReportListDetail", new ReportListModel());
        }
        [HttpPost]
        public JsonResult SaveData(ReportListModel model)
        {
            string msg = null;
            if (ModelState.IsValid)
            {
                msg = model.ReportListId > 0 ? _departmentRepository.Update(model.FromModel()) : _departmentRepository.Insert(model.FromModel());
            }
            return Json(new { message = msg });
        }
    }
}
